package com.marketing.auth.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis-Plus配置类
 */
@Configuration
public class MybatisPlusConfig {

    private static final Logger logger = LoggerFactory.getLogger(MybatisPlusConfig.class);

    /**
     * 分页插件配置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        logger.info("初始化MyBatis-Plus分页插件");
        
        try {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            
            // 添加分页插件
            PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
            
            // 设置分页参数
            paginationInterceptor.setMaxLimit(1000L); // 最大分页限制
            paginationInterceptor.setOverflow(false); // 溢出总页数后是否进行处理
            
            interceptor.addInnerInterceptor(paginationInterceptor);
            
            logger.info("MyBatis-Plus分页插件初始化成功，数据库类型: MySQL, 最大分页限制: 1000");
            return interceptor;
            
        } catch (Exception e) {
            logger.error("MyBatis-Plus分页插件初始化失败: {}", e.getMessage(), e);
            throw new RuntimeException("MyBatis-Plus配置失败", e);
        }
    }
}